package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.MemberDto;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.ResponseDataDto;
import cevent.source.cloudcenter.server.dto.ShortMessageDto;
import cevent.source.cloudcenter.server.service.MemberService;
import cevent.source.cloudcenter.server.service.ShortMessageService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 短信管理表控制层
 */
@RestController
@RequestMapping("/admin/shortMessage")
public class ShortMessageController {
    private static final Logger LOG = LoggerFactory.getLogger(ShortMessageController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 短信管理表";
    @Resource
    private ShortMessageService shortMessageService;
    @Resource
    private MemberService memberService;

    //1.dto-shortMessage获取
    @RequestMapping("/dtoList")
    public List<ShortMessageDto> getDtoList() {
        return shortMessageService.shortMessageDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        shortMessageService.getShortMessagePageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody ShortMessageDto shortMessageDto) {
        LOG.info("传入的shortMessage DTO：{}", shortMessageDto);

        ValidatorUtil.requiredEmpty(shortMessageDto.getMobile(), "手机号");
        ValidatorUtil.requiredLength(shortMessageDto.getMobile(), "手机号", 11, 11);
        /*ValidatorUtil.requiredEmpty(shortMessageDto.getCode(), "验证码");*/
        ValidatorUtil.requiredEmpty(shortMessageDto.getContent(), "发送内容");
        ValidatorUtil.requiredLength(shortMessageDto.getContent(), "发送内容", 3, 255);
       /* ValidatorUtil.requiredEmpty(shortMessageDto.getUse(), "用途");
        ValidatorUtil.requiredEmpty(shortMessageDto.getStatus(), "状态");
        ValidatorUtil.requiredEmpty(shortMessageDto.getTimes(), "生效时间");*/
        ResponseDataDto responseData = new ResponseDataDto();
        shortMessageService.save(shortMessageDto);
        responseData.setResponseData(shortMessageDto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的shortMessage uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        shortMessageService.delete(uniId);
        return responseData;
    }

    //发送短信
    @RequestMapping(value="/send",method = RequestMethod.POST)
    public ResponseDataDto sendShortMessage(@RequestBody ShortMessageDto shortMessageDto){
        ResponseDataDto responseData=new ResponseDataDto();
        shortMessageService.saveAndSendShortMessage(shortMessageDto);
        responseData.setResponseData("短信发送成功，发送号码：{}"+shortMessageDto.getMobile());
        return responseData;
    }
    //验证手机号是否存在
    @RequestMapping("/mobileExist/{mobile}")
    public ResponseDataDto checkModileExist(@PathVariable String mobile){
        ResponseDataDto responseData=new ResponseDataDto();
        MemberDto memberDto=memberService.getMemberPhone(mobile);
        //为空在返回false不存在，可以发送验证码
        if(memberDto==null){
            responseData.setSuccess(false);
        }else{
            responseData.setSuccess(true);
        }
        return responseData;
    }
}
